package bhbt.dao.core;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import bhbt.taglib.pagetag.Pagination;
import bhbt.utils.MapUtil;







import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class BaseDao extends SqlSessionDaoSupport {
	
	@Resource
	private SqlSession sqlSession;
	
    /**
	* 添加对象
	* @Title: createInfo
	* @param sqlId sqlId
	* @param paramObj paramObj
	* @return Object
	* @author: Qiyh 2015-09-10
	*/
	public Object createInfo(final String sqlId, final Object paramObj){
		return sqlSession.insert(sqlId, paramObj);
	}
	
	/**
	* 修改对象
	* @Title: updateInfo
	* @param sqlId sqlId
	* @param paramObj paramObj
	* @return boolean
	* @author: Qiyh 2015-09-10
	*/
	public boolean updateInfo(final String sqlId, final Object paramObj){
		int count = sqlSession.update(sqlId, paramObj);
		 
		return count > 0 ? true : false;
	}
	
	public Object sumInfo(final String sqlId, final Object paramObj){
		int count = sqlSession.update(sqlId, paramObj);
		 
		return count > 0 ? true : false;
	}
	
	
	/**
	 * 删除对象
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2015-09-10
	 */
	public boolean deleteInfo(final String sqlId, final Object paramObj){
		int count = sqlSession.delete(sqlId, paramObj);
		return count > 0 ? true : false;
	}
	
	/**
	 * 检查对象是否存在
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return boolean
	 * @author: Qiyh 2015-09-10
	 */
	public boolean isExist(final String sqlId, final Object paramObj){
		List dataList = sqlSession.selectList(sqlId, paramObj);
		if (null!=dataList && 0<dataList.size()) {
			return true;
		}
		return false;
	}
	
	/**
	* 查询对象
	* @Title: queryForObject
	* @param sqlId sqlId
	* @param paramObj paramObj
	* @return Object
	* @author: Qiyh 2015-09-10
	*/
	public <T>T queryForObject(final String sqlId, final Object paramObj){
		return (T)sqlSession.selectOne(sqlId, paramObj);
	}
	
	/**
	* 查询列表
	* @Title: queryForList
	* @param sqlId sqlId
	* @param paramObj paramObj
	* @return List
	* @author: Qiyh 2015-09-10
	*/
	public List queryForList(final String sqlId, final Object paramObj){
		List dataList = sqlSession.selectList(sqlId, paramObj);
		return dataList!=null?dataList:new ArrayList();
	}
	
	/**
	 * 分页查询列表<br>
     * 需要在resultMap里添加totalCount字段映射
	 * @param sqlId sqlId
	 * @param paramObj paramObj
	 * @return Pagination
	 * @author: Qiyh 2015-09-15
	 */
	public Pagination queryForPageList(final String sqlId, final Map<String,Object> paramObj){
		Pagination retPage = new Pagination();
		// 总记录数
		Integer total = (Integer) sqlSession.selectOne(sqlId+"_count", paramObj);
		//结果集
		if (null!=total && 0<total) {
			int page = MapUtil.getIntValue(paramObj, "pagination.page");
			int pageSize = MapUtil.getIntValue(paramObj, "pagination.pageSize");
			pageSize = pageSize<=0?Pagination.DEFULT_PAGE_SIZE:pageSize;
			String orderBy = MapUtil.getString(paramObj, "pagination.orderBy");
			retPage.setPage(page);
			retPage.setPageSize(pageSize);
			retPage.setTotalCount(total);
			retPage.setOrderBy(orderBy);
			paramObj.put("pagination", retPage);
			// 结果集
			List dataList = sqlSession.selectList(sqlId, paramObj);
			retPage.setPageList(dataList);
		}
		return retPage;
	}
	
	/**
	 * 执行存储过程/函数
	 * @param procId procId
	 * @param obj obj
	 * @author: Qiyh 2015-09-10
	 */
	public void execProc(String procId, Object obj){
	    sqlSession.insert(procId, obj);
	}
		
	
}
